import { QueryListQueryBySls, QueryListQuery } from '@/interfaces/request.interface';
import { createParamDecorator } from '@nestjs/common';
import { ORDER } from '@/constants/common.constant';



export const QueryListBySls = createParamDecorator(
  (data, req): QueryListQueryBySls<any> => {
    const { page, pageSize, startDate, endDate, ...other } = req.query;
    const nowDate = Date.now();
    return {
      offset: Number(pageSize || 20),
      line: Number(pageSize || 20) * (Number(page || 1) - 1),
      from: Math.floor((startDate || (nowDate - 7 * 24 * 3600 * 1000)) / 1000),
      to: Math.floor((endDate || nowDate) / 1000),
      query: other,
    }
  },
);


export const QueryList = createParamDecorator(
  (data, req): QueryListQuery<any> => {
    const { page, pageSize, sortKey, sortType, ...query } = req.query;
    return {
      skip: (Number(page || 1) - 1) * Number(pageSize || 20),
      take: Number(pageSize || 20),
      sort: {
        key: sortKey,
        value: ORDER[sortType] || sortType,
      },
      query
    }
  },
);
